/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.agro.editores;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import org.marcelo.agro.core.api.ILotes;
import org.marcelo.agro.entidades.Departamento;
import org.marcelo.agro.entidades.Distrito;
import org.openide.explorer.propertysheet.ExPropertyEditor;
import org.openide.explorer.propertysheet.InplaceEditor;
import org.openide.explorer.propertysheet.PropertyEnv;
import org.openide.explorer.propertysheet.PropertyModel;
import org.openide.util.Lookup;

/**
 *
 * @author marcelo
 */
public class DistritoEditor extends PropertyEditorSupport 
    implements ExPropertyEditor, InplaceEditor.Factory {
    
    private InplaceEditor ed = null;

    @Override
    public void attachEnv(PropertyEnv pe) {
        pe.registerInplaceEditorFactory(this);
    }

    @Override
    public InplaceEditor getInplaceEditor() {
        if (ed == null){
            ed = new Inplace();
        }
        return ed;
    }
    
    private static class Inplace implements InplaceEditor {
        
        private PropertyEditor editor;
        private PropertyModel model;
        private static ILotes il = Lookup.getDefault().lookup(ILotes.class);
        private Departamento dpto = Lookup.getDefault().lookup(Departamento.class);
        private JComboBox<Distrito> comboDistrito = null;

        
        @Override
        public void connect(PropertyEditor pe, PropertyEnv pe1) {
           editor = pe;
           reset();
        }

        @Override
        public JComponent getComponent() {
            if (comboDistrito == null) {
                try {
                    Distrito[] distritos = il.getDistritos().toArray(new Distrito[il.getDistritos(dpto).size()] );
                    comboDistrito = new JComboBox<Distrito>(distritos);
                }
                catch (Exception mostrar) {}
            }
            return comboDistrito;
        }

        @Override
        public void clear() {
            editor = null;
            model= null;
        }

        @Override
        public Object getValue() {
            return this.comboDistrito.getSelectedItem();
        }

        @Override
        public void setValue(Object o) {
            this.comboDistrito.setSelectedItem(o);            
        }

        @Override
        public boolean supportsTextEntry() {
            return false;
        }

        @Override
        public void reset() {
            Distrito distrito = (Distrito) editor.getValue();
            if (distrito != null) {
                if (comboDistrito == null) {
                     ((JComboBox<Distrito>) getComponent()).setSelectedItem(distrito);
                }
            }
            
        }

        @Override
        public void addActionListener(ActionListener al) {
            //No se necesita
        }

        @Override
        public void removeActionListener(ActionListener al) {
            //No se necesita
        }

        @Override
        public KeyStroke[] getKeyStrokes() {
             return new KeyStroke[0];
        }

        @Override
        public PropertyEditor getPropertyEditor() {
            return this.editor;
        }

        @Override
        public PropertyModel getPropertyModel() {
            return this.model;
        }

        @Override
        public void setPropertyModel(PropertyModel pm) {
            this.model = pm;
        }

        @Override
        public boolean isKnownComponent(Component cmpnt) {
           //Probar
            return cmpnt == comboDistrito || comboDistrito.isAncestorOf(cmpnt);
        }
    
    }
}


